package com.atguigu.flink.watermark;

import com.atguigu.flink.function.WaterSensorMapFunction;
import com.atguigu.flink.pojo.WaterSensor;
import org.apache.flink.api.common.eventtime.*;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.TimerService;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;

/**
        自定义水印生成策略。

 */
public class Demo6_CustomWaterMark
{
    public static void main(String[] args) {

       Configuration conf = new Configuration();
       conf.setInteger("rest.port", 3333);
       StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);

        env.getConfig().setAutoWatermarkInterval(2000);

        //创建水印生成策略
        WatermarkStrategy<WaterSensor> watermarkStrategy = WatermarkStrategy
            //产生连续(从小到大，不存在乱序)的水印
            .<WaterSensor>forGenerator(new WatermarkGeneratorSupplier<WaterSensor>()
            {
                //返回一个WatermarkGenerator
                @Override
                public WatermarkGenerator<WaterSensor> createWatermarkGenerator(Context context) {
                    return new MyWMG();
                }
            })
            .withTimestampAssigner(new SerializableTimestampAssigner<WaterSensor>()
            {
                //从数据中，选择哪个时间属性制作水印
                @Override
                public long extractTimestamp(WaterSensor element, long recordTimestamp) {
                    return element.getTs();
                }
            });

        env.setParallelism(1);


        env
                   .socketTextStream("hadoop103", 8888)
                   .map(new WaterSensorMapFunction())
                   .assignTimestampsAndWatermarks(watermarkStrategy)
                   .process(new ProcessFunction<WaterSensor, WaterSensor>()
                   {
                       //来一条数据就触发一次
                       @Override
                       public void processElement(WaterSensor value, Context ctx, Collector<WaterSensor> out) throws Exception {
                           TimerService timerService = ctx.timerService();

                           System.out.println("当前数据达到时，process算子的水印(主观时间):"+timerService.currentWatermark());
                           System.out.println("当前数据达到时，process算子的客观时间:"+timerService.currentProcessingTime());

                           out.collect(value);

                       }
                   }).startNewChain()
                   .print();


                try {
                            env.execute();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

    }

    //可以自动推迟3s的
    private static class  MyWMG implements WatermarkGenerator<WaterSensor>{

        //保证水印单调递增，水印只能增大，不能变小
        private long maxTimestamp;

        //模拟间歇性产生水印
        @Override
        public void onEvent(WaterSensor event, long eventTimestamp, WatermarkOutput output) {
            //保证水印(时间)不会回退
            maxTimestamp = Math.max(maxTimestamp, eventTimestamp);
            /*Watermark watermark = new Watermark(maxTimestamp - 3000);
            System.out.println("向下游发送:"+watermark.getTimestamp());
            output.emitWatermark(watermark);*/
        }

        //周期性
        @Override
        public void onPeriodicEmit(WatermarkOutput output) {
            Watermark watermark = new Watermark(maxTimestamp - 3000);
            System.out.println("向下游发送:"+watermark.getTimestamp());
            output.emitWatermark(watermark);
        }
    }
}
